package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ArticleDTO;
import com.ruoyi.system.domain.WaImage;
import com.ruoyi.system.domain.WaNotice;
import com.ruoyi.system.domain.vo.ArticleVo;
import com.ruoyi.system.service.WaImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/publish/image")
public class WaImageController  extends BaseController {
    @Autowired
    private WaImageService imageService;
    //  获取图片新闻列表
    @PreAuthorize("@ss.hasPermi('system:image:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(WaImage image)
    {
        startPage();
        List<WaImage> images = imageService.selectImageList(image);
        return getDataTable(images);
    }
    //通过栏目id获取文章
    @PreAuthorize("@ss.hasPermi('system:image:article')")
    @GetMapping("/article")
    public AjaxResult articleList(@RequestParam("columnId") Long columnId)
    {
        try {
            // 参数校验
            if (columnId == null || columnId <= 0) {
                return error("栏目ID不能为空且必须为正数");
            }
            // 构造查询条件对象
            WaImage query = new WaImage();
            // 查询文章列表
            List<WaImage> articles = imageService.selectArticleByColumnById(query);
            return success(articles);
        } catch (Exception e) {
            return error("获取文章列表失败，请稍后重试");
        }
    }
//添加图片新闻
@PreAuthorize("@ss.hasPermi('system:image:add')")
    @Log(title = "图片新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WaImage image)
    {
        Long articleId = image.getArticleId();
        if(articleId == null || articleId<=0);
        if(imageService.isArticleUsedImage(articleId)){
            return error("该文章id已被图片新闻使用，请选择其他文章");
        }
        image.setCreateBy(getUsername());
        return toAjax(imageService.insertImage(image));
    }
    //删除图片新闻
    @PreAuthorize("@ss.hasPermi('system:image:remove')")
    @Log(title = "通知公告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{imageId}")
    public AjaxResult remove(@PathVariable Long imageId)
    {
        return toAjax(imageService.deleteImageById(imageId));
    }

//
@GetMapping("/articles")
public AjaxResult selectArticle(ArticleDTO dto) {
    List<ArticleVo> list = imageService.selectArticle(dto);
    return success(imageService.selectArticle(dto));
}
}
